package com.ustc.hewei.consumerweb.controller;


import com.ustc.hewei.commonmodule.dto.Result;
import com.ustc.hewei.commonmodule.entity.Shop;
import com.ustc.hewei.commonmodule.service.IShopService;
import com.ustc.hewei.commonmodule.utils.Snowflake;
import com.ustc.hewei.nettyrpc2_0.common.annotation.AutowireService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hewei
 * @since 2022-10-05
 */
@RestController
@RequestMapping("/shop")
public class ShopController {

    public IShopService getShopService() {
        return shopService;
    }

    public void setShopService(IShopService shopService) {
        this.shopService = shopService;
    }

    @Resource
    private IShopService shopService;

    @GetMapping("/of/type")
    public Result queryShopByType(
            @RequestParam("typeId") Integer typeId,
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam(value = "x", required = false) Double x,
            @RequestParam(value = "y", required = false) Double y
    ) {
        return shopService.queryShopByType(typeId, current, x, y);
    }

    @GetMapping("/{id}")
    public Result queryShopById(@PathVariable("id") Long id) {
        return shopService.queryById(id);
    }

    @PutMapping
    public Result updateShop(@RequestBody Shop shop) {
        // 写入数据库
        return shopService.update(shop);
    }

    @GetMapping("/addAllShopToCache")
    public void addAllShopToCache() {
        shopService.addAllShopToCache();
    }

    @GetMapping("/of/name")
    public Result queryByNameLike(@RequestParam (value = "current", defaultValue = "1") Integer current,
                                  @RequestParam (value = "name", required = false) String name) {
        return shopService.queryByNameLike(name, current);
    }
}

